import os
import logging
from dotenv import load_dotenv

class Settings:
    """应用配置类
    负责加载环境变量和配置日志
    """
    def __init__(self):
        """初始化配置，加载环境变量并配置日志"""
        self._load_env()
        self._setup_logging()
    
    def _load_env(self):
        """加载.env文件中的环境变量"""
        load_dotenv()
    
    def _setup_logging(self):
        """配置日志系统"""
        log_level = os.getenv("LOG_LEVEL", "INFO")
        logging.basicConfig(
            level=getattr(logging, log_level),
            format="%(asctime)s - %(name)s - %(levelname)s - %(message)s"
        )
    
    def get_db_uri(self):
        """获取数据库连接URI
        
        Returns:
            str: 数据库连接URI
        """
        return os.getenv("DB_URI")
    
    def get_allowed_tables(self):
        """获取允许访问的表列表
        
        Returns:
            list: 允许访问的表名列表
        """
        tables = os.getenv("ALLOWED_TABLES", "").split(",")
        return tables if tables[0] else []
    
    def get_max_history(self):
        """获取最大历史记录数
        
        Returns:
            int: 最大历史记录数
        """
        return int(os.getenv("MAX_HISTORY", 10))
    
    def get_ollama_config(self):
        """获取Ollama模型配置
        
        Returns:
            dict: 包含模型名称和URL的配置字典
        """
        return {
            "model": os.getenv("OLLAMA_MODEL", "qwen3:0.6b"),
            "url": os.getenv("OLLAMA_URL", "http://172.30.160.1:11434")
        }

# 创建全局配置实例
settings = Settings()